package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class LIstViewActivity extends AppCompatActivity {
    private String[] names = {"张三", "李四", "王五", "赵六", "孙七", "周八", "吴九"};
    private String[] ages = {"18", "19", "20", "21", "22", "23", "24"};
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_list_view);
        listView = findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter());
        //单击事件
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Toast.makeText(this, "姓名："+names[i] +","+"年龄："+ages[i], Toast.LENGTH_SHORT).show();
        });
        //长按事件
        listView.setOnItemLongClickListener((adapterView, view, i, l) -> {
            Toast.makeText(this, "长按姓名："+names[i] +","+"年龄："+ages[i], Toast.LENGTH_SHORT).show();
            return true;
        });
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int i) {
            return names[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //数据优化，将缓存利用起来
            MyViewHolder myViewHolder = null;
            if (view == null) {
                //加载布局文件
                view = View.inflate(LIstViewActivity.this, R.layout.item_list, null);
                //加载控件
                myViewHolder = new MyViewHolder();
                myViewHolder.imageView = view.findViewById(R.id.imageView);
                myViewHolder.name = view.findViewById(R.id.name);
                myViewHolder.age = view.findViewById(R.id.age);
                //保存控件
                view.setTag(myViewHolder);
            }else {
                myViewHolder = (MyViewHolder) view.getTag();
            }

            myViewHolder.imageView.setImageResource(R.drawable.ic_launcher_foreground);
            myViewHolder.name.setText(names[i]);
            myViewHolder.age.setText(ages[i]);
            return view;

        }
        class MyViewHolder {
            ImageView imageView;
            TextView name;
            TextView age;
        }
    }
}